package com.noah.note.presenter;

import android.widget.Toast;

import com.noah.note.R;
import com.noah.note.bean.Reply;
import com.noah.note.bean.ReplyNote;
import com.noah.note.bean.User;
import com.noah.note.global.BasePresenter;
import com.noah.note.global.Const;
import com.noah.note.global.OkHttpCallback;
import com.noah.note.manager.JsonManager;
import com.noah.note.manager.LoginManager;
import com.noah.note.model.MarkModel;
import com.noah.note.utils.LogUtils;
import com.noah.note.utils.NetUtils;
import com.noah.note.utils.ToastUtils;
import com.noah.note.view.MarkView;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MarkPresenter extends BasePresenter<MarkView> {
    MarkModel model;

    public MarkPresenter() {
        model = new MarkModel();
    }

    public void getMarkList(long time, final boolean isRefresh) {
        if (!NetUtils.isConnected()) {//无网络
            ToastUtils.show(R.string.failure_network);
            return;
        }
        User user = LoginManager.getInstance().getUser();
        if (user == null) {
            return;
        }
        model.getMarkList(user.getUid(), time, new OkHttpCallback() {
            @Override
            public void onFail(Reply reply) {
                switch (reply.getCode()) {
                    case Const.FAIL:
                        if (getView() != null) {
                            if (isRefresh) {
                                getView().markListRefreshSuccess(null);
                            } else {
                                getView().markListSuccess(null);
                            }
                        }
                        break;
                    case Const.FAIL_REQUEST:
                        ToastUtils.show(R.string.failure_request);
                        break;
                }
            }

            @Override
            public void onSuccess(Reply reply) {
                String data = reply.getData();
                ArrayList<ReplyNote> replyNotes = (ArrayList<ReplyNote>) JsonManager.getInstance().toBeanList(data, ReplyNote.class);
                LogUtils.i(replyNotes.toString());
                if (getView() != null) {
                    if (isRefresh) {
                        getView().markListRefreshSuccess(replyNotes);
                    } else {
                        getView().markListSuccess(replyNotes);
                    }
                }
            }
        });
    }
}
